#ifndef _TEXTBOX_H_
#define _TEXTBOX_H_

#pragma once
#include "Component.h"
#include "SDL.h"
#include "SDL_ttf.h"
#include "SDL_gfxPrimitives.h"
#include <string>
#include "../../FrameTimer.h"
#include "../../SDL_Exception.h"

#define FONT_SIZE 18;
//Space between first character rendered and border of textbox,
//values over or equal to 4 recommended.
#define MARGIN 4
#define CURSOR_BLINK_RATE 500
//Colors
#define CYAN 0x99FFCC
#define BLACK 0x000000FF
#define DARK_GREY 0x333333
#define WHITE 0xFFFFFF
#define ORANGE 0xFFCA12

class TextBox : public Component {
private:
	int yMargin;
	bool selected;
	bool textChanged, cursorMoved;

	FrameTimer cursorTimer;
	Sint16 screenPosCursor;
	size_t stringPosCursor;

    std::string showText;
    SDL_Color textColor;
	SDL_Rect widArea;  // Rendered copy of widget area
	SDL_Rect textSrcArea;
	SDL_Rect textDstArea;

    TTF_Font* font;

    SDL_Surface* renderedText;

	/**
	 * Initializes class resources.
	 */
	void init();
	/**
	 * Free class resources.
	 */
    void cleanUp();
	/**
	 * Draws background box.
	 */
	void drawBox(SDL_Surface* s);
    /**
     * Returns the width of the text rendered with the class font
     */
    int getTextWidth(const std::string& txt);
    /**
     * Renderizes the text.
     */
	void drawText(SDL_Surface* s);
	/**
	 * Draws the cursor.
	 */
	void drawCursor(SDL_Surface* s);
public:
	TextBox();
	TextBox( Sint16 posX, Sint16 posY, Uint16 width, Uint16 height);
	TextBox( Sint16 posX, Sint16 posY, Uint16 width, Uint16 height, const std::string& defaultText);
	virtual ~TextBox();

	/**
	According to the event received sets to show the character
	for the button pressed or gets active by getting clicked.
	**/
	virtual void handleEvent(SDL_Event* event);
	/**
	 * Prepares the widget to be renderized
	 */
	virtual void show();
	/**
	Modifies value of selected attribute.
	**/
	void setSelected(bool select);
	/**
	Draws the textbox.
	**/
	void draw(SDL_Surface* screen);
	/**
	Sets the text received as the content of the textbox.
	**/
	void setText(const std::string& t);
	/**
	Returns the text contained in the textbox.
	**/
	std::string getText();
	/**
	Empty the box
	**/
	void clearBox();
	/**
	Verifica que se hizo click en la caja.
	**/
	bool isClicked(SDL_Event* event);
};

#endif
